package com.fessc.develop.handler;

import com.fessc.develop.entity.dbvo.SettleTrade;
import com.fessc.develop.exception.BusinessException;
import com.fessc.develop.handler.processor.send.ISendProcessor;
import com.fessc.develop.model.enums.EnumBranch;
import com.fessc.develop.utils.BusiUtil;
import com.fessc.develop.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SettleTradePushHandler {
    @Resource
    private List<ISendProcessor> settleTradeProcessors;

    private Map<String, ISendProcessor> processorMap;

    @PostConstruct
    public void init() {
        if (BusiUtil.isNotNull(settleTradeProcessors)) {
            processorMap = settleTradeProcessors.stream().collect(Collectors.toMap(ISendProcessor::getProcessorForBranch, p->p, (v1, v2) -> v1));
        } else {
            processorMap = new HashMap<>();
        }
    }

    public void settleTradePush(List<SettleTrade> settleTrades) {
        //按照机构分类处理
        Map<Integer, List<SettleTrade>> crossBorderMap = settleTrades.stream().collect(Collectors.toMap(SettleTrade::getBankid,
                p -> {
                    List<SettleTrade> list = new ArrayList<>();
                    list.add(p);
                    return list;
                },
                (List<SettleTrade> k1, List<SettleTrade> k2) -> {
                    k1.addAll(k2);
                    return k1;
                }));

        //加工构造推送报文，调用接口推送
        settleTradePushProcess(crossBorderMap);

    }

    private void settleTradePushProcess(Map<Integer, List<SettleTrade>> crossBorderMap) {
        for (Map.Entry<Integer, List<SettleTrade>> entry : crossBorderMap.entrySet())  {
            try {
                ISendProcessor processor = BusiUtil.isNull(processorMap.get(entry.getKey().toString()))? processorMap.get(EnumBranch.COMMON.toString()) : processorMap.get(entry.getKey().toString());
                if (BusiUtil.isNull(processor)) {
                    throw ExceptionUtil.businessException("EE4001");
                }
                processor.settleTradeProcess(entry.getValue()); //预付款信息推送
            } catch (Exception e) {
                if (e instanceof BusinessException) {
                    log.error(((BusinessException) e).getResult().getMsg(), e);
                } else {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    public void settleTradeStatusPush(List<SettleTrade> SettleTrades, String pushType) {
        Map<String, List<SettleTrade>> settleTradeMap;
        settleTradeMap = SettleTrades.stream().collect(Collectors.toMap(item -> item.getBankid().toString(),
                p -> {
                    List<SettleTrade> list = new ArrayList<>();
                    list.add(p);
                    return list;
                },
                (List<SettleTrade> k1, List<SettleTrade> k2) -> {
                    k1.addAll(k2);
                    return k1;
                }));

        //加工构造推送报文，调用接口推送
        settleTradeStatusProcessor(settleTradeMap);

    }

    private void settleTradeStatusProcessor(Map<String, List<SettleTrade>> settleTradeMap) {
        for (Map.Entry<String, List<SettleTrade>> entry : settleTradeMap.entrySet())  {
            try {
                ISendProcessor processor = BusiUtil.isNull(processorMap.get(entry.getKey()))? processorMap.get(EnumBranch.COMMON.toString()) : processorMap.get(entry.getKey());
                if (BusiUtil.isNull(processor)) {
                    throw ExceptionUtil.businessException("EE4001");
                }
                processor.settleTradeStatusProcess(entry.getValue()); //跨境付汇状态信息推送
            } catch (Exception e) {
                if (e instanceof BusinessException) {
                    log.error(((BusinessException) e).getResult().getMsg(), e);
                } else {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

}
